package aliasMatching.controller;

import aliasMatching.model.DisplayInfo;
import aliasMatching.model.MyTableModel;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;

import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.JPanel;
import javax.swing.JScrollBar;
import javax.swing.JScrollPane;

/**
 *
 * @author amendrashrestha
 */
public class TableActions extends AbstractAction {

    List List;
    List clickedUser;
    MyTableModel mdl;
    int tableSize;
    public static JPanel panel;
    public static JPanel timeGraph;
    JScrollBar scroller;
    CreateGraph graphCreator;

    public TableActions(String text, String actionCommand, List List,
            List clickedUser, MyTableModel mdl, int size, JPanel p) {
        super(text);
        putValue(Action.ACTION_COMMAND_KEY, actionCommand);
        this.List = List;
        this.clickedUser = clickedUser;
        this.mdl = mdl;
        this.tableSize = size;
        this.panel = p;
        scroller = new JScrollBar();
        graphCreator = new CreateGraph();
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        
        if ("postTimeGraph".equals(getValue(Action.ACTION_COMMAND_KEY).toString())) {
//            if (graph != null) {
//                panel.remove(graph);
//            }
            timeGraph = graphCreator.createTimegraphPanel(clickedUser);
            panel.add(timeGraph, BorderLayout.SOUTH);
            panel.updateUI();
            panel.repaint();
        }

        if ("averageMatch".equalsIgnoreCase(getValue(Action.ACTION_COMMAND_KEY).toString())) {

            float total = 0.0f;

            for (int i = 0; i < List.size(); i++) {
                total += Float.valueOf(List.get(i).toString());
            }
            float average = total / List.size();

            DisplayInfo di = new DisplayInfo("Total", average);
            mdl.SetValue(di);
        }
    }
}
